<?php

class BaseController {
	protected $get = array();
	public $controller;
	public $action;

	public $pageTitle = '注册码';							// html meta-title
	public $pageKeywords = '注册码';							// html meta-keywords
	public $pageContent = '注册码';								// html meta-content

	public $haderTpl = 'header';							// 页面头名称
	public $footerTpl = 'footer';							// 页面尾名称
	public $publicDir = 'public';							// 页面目录
	public $css = array();									// 页面目录
	public $js = array();									// 页面目录
	public $pageData=array();

	private $view;

	function __construct() {
		$this->get = KJ::params();
		$this->controller = KJ::controller();
		$this->action = KJ::action();

		$this->css = array(
			// 'wap_base' => '',
		);

		$this->js = array(
			// 'zepto' => '',
		);
	}

	function assign(array $assign=array()) {
		$this->initView();

		if(!empty($assign))
			foreach ($assign as $key => $value) {
				$this->view->assign($key, $value);
			}
	}

	/*
	 * key 键
	 * value 值
	 */
	function addData($key=null, $value=null) {
		if(is_null($key) || empty($key)) {
			return false;
		}

		if(is_array($key)) {
			foreach ($key as $k => $v) {
				!is_numeric($k) && $this->pageData[$k] = $v;
			}
		}

		$this->pageData[$key] = $value;
	}

	function display($html=null, $publicHtml = true) {

		if(empty($html)) {
			$html = $this->controller.DIRECTORY_SEPARATOR.$this->action;
		} else {
			if($len = strrpos($html, '.')) {
				$html = mb_substr($html, 0, $len);
			}
		}
		
		$template = KJ_PATH.'view'.DIRECTORY_SEPARATOR.strtolower($html.'.php');
		if(!file_exists($template)) {
			die($template.' 模板不存在');
		}

		extract($this->pageData, EXTR_SKIP);

		// $publicHtml && !empty($this->haderTpl) && include KJ_PATH.'view'.DIRECTORY_SEPARATOR.strtolower($this->publicDir.DIRECTORY_SEPARATOR.$this->haderTpl.'.php');
		// // 直接引入文件，停止smarty模板引用，原因：服务器没有write权限
		// include $template;

		//$publicHtml && !empty($this->footerTpl)  &&  include KJ_PATH.'view'.DIRECTORY_SEPARATOR.strtolower($this->publicDir.DIRECTORY_SEPARATOR.$this->footerTpl.'.php');

		// die;
		
		!$this->view && $this->initView();
		$this->view->display($template);
	}

	function success() {

	}

	function failed() {

	}

	function redirect($url) {
		header('location:'.$url);
		die;
	}

	function initView() {
		$file = KJ_PATH.'extension'.DIRECTORY_SEPARATOR.'smarty'.DIRECTORY_SEPARATOR.'Smarty.class.php';
		$this->view = KJ::instance($file, 'Smarty');

		$this->view->caching = ISWRITE;
		$this->view->template_dir = $GLOBALS["pac"]["smarty"]["template_dir"];
		$this->view->config_dir = $GLOBALS["pac"]["smarty"]["config_dir"];
		$this->view->cache_dir = $GLOBALS["pac"]["smarty"]["cache_dir"];
		$this->view->compile_dir = $GLOBALS["pac"]["smarty"]["compile_dir"];
		$this->view->debugging = $GLOBALS["pac"]["smarty"]["debugging"];
	}

	function isPostRequest() {
		return (isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post');
	}

	function isGetRequest() {
		return (isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get');
	}

	function isAjaxRequest() {
		return (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest");
	}
}